//
//  LHHomePageController.m
//  EPA
//
//  Created by 卢浩 on 16/4/15.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHHomePageController.h"
#import "LHMenuViewController.h"
#import "LHTopicsViewController.h"
#import "LHNetWorkRequestManager.h"
#import "LHReadingViewController.h"
#import "LHProductsTableViewController.h"
#import "LHRedioTableViewController.h"
#import "LHUserInfoManager.h"
#import "TouchView.h"

@interface LHHomePageController ()<MenuClickDelegate>

@property (nonatomic,strong) TouchView *touchView;

@end

@implementation LHHomePageController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[LHMenuViewController getMenuViewController] removePanGesture];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[LHMenuViewController getMenuViewController] addPanGesture];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@",NSHomeDirectory());

    self.navigationController.navigationBar.barTintColor = [UIColor darkGrayColor];
//    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"首页(才艺画板)";
    
    //设置协议的代理
    [LHMenuViewController getMenuViewController].menuDelegate = self;
    //设置菜单列表的内容
    [LHMenuViewController getMenuViewController].menuArray = @[@"首页·画板",@"电台·RADIO",@"话题·TOPICS",@"阅读·READING",@"良品·PRODUCTS"].mutableCopy;
    
    self.touchView = [[TouchView alloc]initWithFrame:CGRectMake(2, 66, self.view.frame.size.width - 4, self.view.frame.size.height - 68)];
    self.touchView.backgroundColor = [UIColor whiteColor];
    //设置画板边框
    self.touchView.layer.borderWidth = 2;
    self.touchView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    //开启用户交互
//    self.touchView.userInteractionEnabled = YES;
    [self.view addSubview:self.touchView];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空画板" style:(UIBarButtonItemStylePlain) target:self action:@selector(clearPalette)];
}

//清空画板
- (void)clearPalette{
    [self.touchView.allPoints removeAllObjects];
    [self.touchView.points removeAllObjects];
    [self.touchView setNeedsDisplay];
}

- (void)showAlertControllerWithStr:(NSString *)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:(UIAlertControllerStyleAlert)];
        [alertController addAction:[UIAlertAction actionWithTitle:@"前往登陆页面" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [[LHMenuViewController getMenuViewController] showLoginVC];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
    });
}

//点击菜单项的代理
- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title{
    [self.navigationController popToRootViewControllerAnimated:NO];
//    if (index != 0) {
//        if (![LHUserInfoManager shareManager].uid) {
//            [self showAlertControllerWithStr:@"进入此页面,需要先登录哦!"];
//            return;
//        }
//    }
    switch (index) {
        case 0:{
            break;
        }
        case 1:{
            LHRedioTableViewController *redioVC = [[LHRedioTableViewController alloc]init];
            [self.navigationController pushViewController:redioVC animated:YES];
            break;
        }
        case 2:{
            LHTopicsViewController *topicsVC = [[LHTopicsViewController alloc]init];
            [self.navigationController pushViewController:topicsVC animated:YES];
            break;
        }
        case 3:{
            LHReadingViewController *readingVC = [[LHReadingViewController alloc]init];
            [self.navigationController pushViewController:readingVC animated:YES];
            break;
        }
        case 4:{
            LHProductsTableViewController *productsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LHProductsTableViewController"];
            [self.navigationController pushViewController:productsVC animated:YES];
            break;
        }
        default:
            break;
    }

}

//点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    if ([LHMenuViewController getMenuViewController].isShowing) {
        [[LHMenuViewController getMenuViewController] hideLeftViewController];
    }else{
        [[LHMenuViewController getMenuViewController] showLeftViewController];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
